# -*- coding=utf-8 -*-
# hadoop 课程地址：https://edu.hellobi.com/course/109/lessons
import re
import urllib.request
from bs4 import BeautifulSoup
from lxml import etree

request_url="https://edu.hellobi.com/course/109/lessons"

if __name__ == '__main__':

    list_date=[]

    request_url_encode=urllib.request.urlopen(request_url).read()
    request_url_decode=request_url_encode.decode("utf8")
    #print(request_url_decode)

    regex="course-detail-content([\s\S]*?)授课讲师"

    request_content_regex=re.compile(regex).findall(request_url_decode)

    soup = BeautifulSoup(request_content_regex[0], 'html.parser')

    lis = soup.find_all(name="li")

    for li in lis:
        #print(li)
        _soup = BeautifulSoup(str(li), 'html.parser')
        #spans = _soup.find_all(name="span")
        #content_etree=etree.HTML(str(spans))
        #
        #print(len(content_etree.xpath('//span[@class="title"]')))
        #title=content_etree.xpath('//[./class="title"]/')
        #print(title)
        #if(len(spans)!=0):
            #print(spans[0].getText())
            #print()

        content_etree = etree.HTML(str(_soup))
        title=content_etree.xpath('//span[@class="title"]')[0].text;
        if(len(content_etree.xpath('//span[@class="date"]'))>0):
            date = content_etree.xpath('//span[@class="date"]')[0].text;
        else:
            date = "00:00";

        list_date.append(date)

        #打印所有的title和date
        #print(title+"----------------"+date)

    print(list_date)

    sum_date=0


    # for date in list_date:
    #     date_list=date.split(":")
    #     date_min=int(date_list[0])
    #     date_second=int(date_list[1])
    #
    #     print(date+"==============="+str(date_min*60+date_second))

    for index in range(len(list_date)):
        #假设已经读到了课时25
        if (index>=0):
            date=list_date[index]
            date_list = date.split(":")
            date_min = int(date_list[0])
            date_second = int(date_list[1])

        # if(list_date[index]=="59:57"):
        #     print(str(index)+"====>"+date + "===============" + str(date_min * 60 + date_second))
            sum_date=sum_date+int(date_min * 60 + date_second)
        else:
            date_min=0
            date_second=0

        print("序号："+str(index) + "====>" + date + "<===============" + str(date_min * 60 + date_second)+"，合计时长为（单位分钟）："+str(sum_date))

    print("共计剩余时长为="+str(sum_date/60)+"分钟")

    print("共计剩余时长为=" + str(sum_date / 3600) + "小时")

